var Chat = {
    ws: null,
    board: null,
    room: 0,
    url: '',
    user: {},
    timer: 0,
    reback_times: 3,
    debug: true,

    start: function(url, boardId) {
        this.url = url
        this.board = $('#' + boardId);
        ws = new WebSocket(url)
        ws.binaryType = "arraybuffer";
        var obj = this

        ws.onopen = function(evt) {
            clearInterval(obj.timer);
            obj.log('server connected');
            obj.log(evt);
            obj.board.html('');
        }
        ws.onclose = function(evt) {
            obj.timer = setInterval(function(){
                if (obj.reback_times == 0) {
                    clearInterval(obj.timer)
                    obj.timer = null;
                } else {
                    obj.reback_times--;
                    obj.start(obj.url)
                }
            }, 5000)
            obj.log('server disconnected');
            obj.log(evt);
        }
        ws.onerror = function(evt) {
            obj.log('server error:' + evt.type);
            obj.log(evt);
        }
        ws.onmessage = function(evt) {
            if (!evt.data) return;
            obj.log(evt);

            var total_data = new DataView(evt.data);
            var total_len = total_data.byteLength;
            if (total_len < 4) {
                obj.log('data error');
                ws.close()
                return;
            }
            
            var off = 0;
            var body = '';
            while(total_len > off) {
                var len = total_data.getUint32(off);
                var data = evt.data.slice(off, off + len + 4);
                body = Packet.unpack(data);
                if (body.code == ErrorCode.SUCCESS) {
                    obj.recvCmd(body.data);
                } else {
                    alert(body.message);
                }
                off += len + 4;
            }
        }

        this.ws = ws
        return this
    },
    log: function(msg) {
        if (this.debug) {
            console.log(msg);
        }
    },
    login: function(user) {
        this.user = user;
        this.send({
            code: ErrorCode.SUCCESS,
            data: {
                method: Methods.USER_LOGIN,
                name: user.name,
            }
        })
    },
    recvCmd: function(data) {
        this.log(data)
        var name = data['name'];
        var content = data['content'];

        this.board.append('<div>'+ name +': '+ content +'</div>');
    },
    send: function(data) {
        this.log('start send data:');
        this.log(data);
        if (this.ws.readyState == this.ws.OPEN) {
            this.ws.send(Packet.pack(data))
        }
    },
    changeRoom: function(room) {
        this.room = room;
        this.board.empty();
        this.send({
            code: ErrorCode.SUCCESS,
            data: {
                method: Methods.USER_ENTER,
                room: room,
            },
        })
    },
    say: function(content) {
        this.send({
            code: ErrorCode.SUCCESS,
            data: {
                method: Methods.USER_SAY,
                content: content,
            }
        })
    },
    close: function() {
        this.log('close ws');
        if (this.ws.readyState == this.ws.OPEN) {
            this.reback_times = 0
            this.send({
                code: ErrorCode.SUCCESS,
                data: {
                    method: Methods.USER_LEAVE,
                }
            })
            this.ws.close()
        }
    }
}